<?php

namespace App\Admin\Metrics\ProjectDesc;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Bar;
use Illuminate\Http\Request;
use App\Models\ProjectUser;
use App\Models\Cabinet;
use App\Models\CabinetAlarmLog;

class DeviceAlertCard extends Bar
{
    /**
     * 初始化卡片内容
     */
    protected $category = [];
    protected $data = [];
    protected $boxes = [];

    protected function init()
    {
        $projects = ProjectUser::where('user_id',Admin::user()->id)->pluck('projectId');
        $boxes = Cabinet::select('id', 'name')->where('pid', 0)->whereIn('project_id',$projects)->get()->toArray();
        $this->boxes = $boxes;

        parent::init();

        $colors = array_slice($this->customColors, 0, count($boxes));

        // 标题
        $this->title('设备报警统计');
        // 设置下拉选项
        // $this->dropdown([
        //     'week' => '本周',
        //     'month' => '本月'
        // ]);
        // 设置图表颜色
        $this->chartColors($colors);
        $this->chartPullRight(false);
        $this->contentWidth(0,12);
    }

    protected function getData($start,$end){
        foreach ($this->boxes as $one) {
            $this->category[] = $one['name'];
            // $this->data[] = DeviceLog::where('device_type', $k)->whereIn('deviceId', $deviceIds)->whereBetween('created_at', [$start, $end])->count();
            $this->data[] = CabinetAlarmLog::where('cabinet_pid', $one['id'])->count();
        }
        // $this->data = array_slice([77, 125, 225, 175, 125, 75, 25], 0, count($this->deviceTypes));
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        switch ($request->get('option')) {
            case 'month':
                $start = date('Y-m').'-01 00:00:00';
                $end = date('Y-m-d H:i:s');
                break;
            case 'week':
                $start = date('Y-m-d H:i:s',strtotime("-7 day"));
                $end = date('Y-m-d H:i:s');
                break;
            default:
                $start = date('Y-m-d H:i:s',strtotime("-7 day"));
                $end = date('Y-m-d H:i:s');
        }
        // 图表数据
        $this->getData($start, $end);
        $this->withChart([
            [
                'name' => '',
                'data' => $this->data,
            ],
        ]);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'chart' => [
                'type' => 'bar'
            ],
            'series' => $data,
            'yaxis' => [
                'show' => true
            ],
            'xaxis' => [
                'type' => 'category',
                'categories' => $this->category,
                'labels' => [
                    'show' => false
                ]
            ],
            'grid' => [
                'show' => true
            ]
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $title
     * @param string $value
     * @param string $style
     *
     * @return $this
     */
    public function withContent($title, $value, $style = 'success')
    {
        // 根据选项显示
        $label = strtolower(
            $this->dropdown[request()->option] ?? 'last 7 days'
        );

        $minHeight = '183px';

        return $this->content(
            <<<HTML
<div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
    <div class="text-left">
        <h1 class="font-lg-2 mt-2 mb-0">{$title}</h1>
        <h5 class="font-medium-2" style="margin-top: 10px;">
            <span class="text-{$style}">{$value} </span>
            <span>vs {$label}</span>
        </h5>
    </div>

    <a href="#" class="btn btn-primary shadow waves-effect waves-light">View Details <i class="feather icon-chevrons-right"></i></a>
</div>
HTML
        );
    }
}
